package com.example;

import org.apache.poi.ss.usermodel.*;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

public class ExcelReader {

    // 读取 D 列（索引 3）的链接
    public static List<String> readPageLinks(String excelPath) throws IOException {
        List<String> links = new ArrayList<>();
        try (FileInputStream inputStream = new FileInputStream(excelPath)) {
            Workbook workbook = new XSSFWorkbook(inputStream);
            Sheet sheet = workbook.getSheetAt(0);

            // 跳过标题行（从第 1 行开始）
            for (int rowNum = 1; rowNum <= sheet.getLastRowNum(); rowNum++) {
                Row row = sheet.getRow(rowNum);
                if (row == null) continue;

                Cell cell = row.getCell(3); // D列索引为3
                if (cell != null && cell.getCellType() == CellType.STRING) {
                    links.add(cell.getStringCellValue().trim());
                }
            }
            workbook.close();
        }
        return links;
    }
    // 读取视频元数据（序号、og:video链接、og:title）
    public static List<VideoMetadata> readVideoMetadata(String excelPath) throws IOException {
        List<VideoMetadata> metadataList = new ArrayList<>();
        try (FileInputStream inputStream = new FileInputStream(excelPath)) {
            Workbook workbook = new XSSFWorkbook(inputStream);
            Sheet sheet = workbook.getSheetAt(0);

            // 遍历数据行（跳过标题行）
            for (int rowNum = 1; rowNum <= sheet.getLastRowNum(); rowNum++) {
                Row row = sheet.getRow(rowNum);
                if (row == null) continue;

                // 提取 og:video 和 og:title
                Cell videoCell = row.getCell(4); // E列（og:video）
                Cell titleCell = row.getCell(5); // F列（og:title）

                if (videoCell != null && titleCell != null) {
                    String videoUrl = videoCell.getStringCellValue().trim();
                    String title = titleCell.getStringCellValue().trim();
                    metadataList.add(new VideoMetadata(rowNum, videoUrl, title));
                }
            }
        }
        return metadataList;
    }

    // 视频元数据对象
    public static class VideoMetadata {
        private final int index;
        private final String videoUrl;
        private final String title;

        public VideoMetadata(int index, String videoUrl, String title) {
            this.index = index;
            this.videoUrl = videoUrl;
            this.title = title;
        }

        // Getter方法
        public int getIndex() { return index; }
        public String getVideoUrl() { return videoUrl; }
        public String getTitle() { return title; }
    }
}